﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FullScreenDemo;

public class HideStatusBarBehavior : PlatformBehavior<Page>
{

#if ANDROID
    protected override void OnAttachedTo(Page bindable, Android.Views.View platformView)
    {
        SetStatusBarVisibility(false);
    }
#endif


#if ANDROID
    protected override void OnDetachedFrom(Page bindable, Android.Views.View platformView)
    {
        SetStatusBarVisibility(true);
    }
#endif


    private static void SetStatusBarVisibility(bool isVisible)
    {
#if ANDROID
        var activity = Platform.CurrentActivity ?? throw new InvalidOperationException("Android Activity can't be null.");
        var window = activity.Window ?? throw new InvalidOperationException($"{nameof(activity.Window)} cannot be null");
#endif
        if (isVisible)
        {
#if ANDROID
            window.ClearFlags(Android.Views.WindowManagerFlags.TranslucentStatus);
            window.ClearFlags(Android.Views.WindowManagerFlags.Fullscreen);
            window.Attributes.LayoutInDisplayCutoutMode = Android.Views.LayoutInDisplayCutoutMode.Default;
#endif
        }
        else
        {
#if ANDROID
            window.Attributes.LayoutInDisplayCutoutMode = Android.Views.LayoutInDisplayCutoutMode.ShortEdges;
            window.AddFlags(Android.Views.WindowManagerFlags.TranslucentStatus);
            window.AddFlags(Android.Views.WindowManagerFlags.Fullscreen);
#endif
        }

    }
}
